package com.staticmethod;

/**
 * Time    : 2023/3/17 12:08
 * Author  : 王摇摆
 * FileName: Test1.java
 * Software: IntelliJ IDEA 2020.2.2
 * Blog    ：https://blog.csdn.net/weixin_44943389?type=blog
 */

public class Test1 {
    public static int publicStaticVariable;//静态共有成员变量
    protected static int protectedStaticVariable;//静态保护成员变量
    private static int privateStaticVariable;//静态私有成员变量
    private int instanceVariable;//实例成员变量

    public static void staticMethod1() {

        //静态方法中可以使用静态变量
        publicStaticVariable = 1;
        protectedStaticVariable = 2;
        privateStaticVariable = 3;

        System.out.println("静态方法1已被调用");
        System.out.println("StaticMethodTest1's publicStaticVariable =" + Test1.publicStaticVariable);
        System.out.println("StaticMethodTest1's protectedStaticVariable =" + Test1.protectedStaticVariable);
        System.out.println("StaticMethodTest1's privateStaticVariable =" + Test1.privateStaticVariable);

        Test1.staticMethod2();// 在静态方法中调用其他静态方法

        /**
         * Non-static field 'instanceVariable' cannot be referenced from a static context
         * 不能在静态方法中引用实例变量
         * instanceVariable = 555;
         */

        /**
         * Non-static method 'instanceMethod()' cannot be referenced from a static context
         * 不能在静态方法中引用实例方法
         * instanceMethod1();
         */

    }

    public static void staticMethod2() {
        System.out.println("静态方法2已被调用");
        privateStaticVariable = 666;
        System.out.println("StaticMethodTest1's privateStaticVariable =" + Test1.privateStaticVariable);

    }

    public void instanceMethod() {
        // 实例方法中使用实例变量
        instanceVariable = 100;
        // 实例方法也可以引用静态变量
        publicStaticVariable = 101;
        protectedStaticVariable = 102;
        privateStaticVariable = 103;

        System.out.println("实例方法已被调用");
        System.out.println("StaticMethodTest1's publicStaticVariable =" + Test1.publicStaticVariable);
        System.out.println("StaticMethodTest1's protectedStaticVariable =" + Test1.protectedStaticVariable);
        System.out.println("StaticMethodTest1's privateStaticVariable =" + Test1.privateStaticVariable);

    }

    public static void main(String[] args) {
        //Test1.staticMethod1();

        Test1 t = new Test1();
        t.instanceMethod();
        System.out.println(t.instanceVariable);

        Test1.staticMethod1();

    }
}
